﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RedisKeyEditor
{
    public partial class StringEditor: UserControl
    {
        public StringEditor()
        {
            InitializeComponent();
            db_encoding.SelectedIndex = 0;

        }

        byte[] data;

        public void setData(byte[] data,long expire)
        {

        }


        private void db_encoding_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(data!=null)
            {
                if(db_encoding.Text=="String")
                {
                    tb_value.Text=Encoding.UTF8.GetString(data);
                }
                else if (db_encoding.Text == "Ascii")
                {
                    String text = Encoding.UTF8.GetString(data);
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < text.Length; i++)
                    {
                        if (text[i] <= 127)
                            sb.Append(text[i]);
                        else
                            sb.AppendFormat("\\x{0:X}", text[i]);
                    }
                    tb_value.Text = sb.ToString();
                }
                else if(db_encoding.Text=="Json")
                {
                    tb_value.Text = Encoding.UTF8.GetString(data); //需要格式化
                }
                else if(db_encoding.Text=="Binary")
                {
                    tb_value.Text = BitConverter.ToString(data);
                }
            }
        }




    }
}
